package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-2-14 10:48
 */
public class 有序数组中的单一元素_540 {
    public int singleNonDuplicate(int[] nums) {
        // 由题意 nums.length != 2
        if (nums.length < 2) {
            return nums[0];
        }
        if (nums[0] != nums[1]) {
            return nums[0];
        }
        if (nums[nums.length-2] != nums[nums.length-1]) {
            return nums[nums.length-1];
        }
        for (int i = 1; i < nums.length-1; i++) {
            if (nums[i-1] != nums[i] && nums[i+1] != nums[i]) {
                return nums[i];
            }
        }
        return -1;
    }

    @Test
    public void testSingleNonDuplicate () {
        int[] nums = {1,1,2,3,3,4,4,8,8};
        System.out.println(singleNonDuplicate(nums));
    }
}
